package com.shangxian.giveithome.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.shangxian.giveithome.code.CommonCode;
import com.shangxian.giveithome.controller.ImgFeiServer;
import com.shangxian.giveithome.controller.MechanismServer;
import com.shangxian.giveithome.controller.UserFeiController;
import com.shangxian.giveithome.entity.PageableDetails;
import com.shangxian.giveithome.entity.Img;
import com.shangxian.giveithome.entity.Mechanism;
import com.shangxian.giveithome.entity.seekhelp.*;
import com.shangxian.giveithome.entity.UserSimple;
import com.shangxian.giveithome.exception.ExceptionUtils;
import com.shangxian.giveithome.mapper.SeekHelpMapper;
import com.shangxian.giveithome.service.SeekHelpService;
import com.shangxian.giveithome.utils.PageUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;


@Service
public class SeekHelpServiceImpl implements SeekHelpService {


    @Resource
    private SeekHelpMapper seekHelpMapper;

    @Resource
    private ImgFeiServer imgFeiClient;

    @Resource
    private UserFeiController userFeiClient;

    @Resource
    private MechanismServer mechanismFeiClient;

    @Override
    public SeekHelpVO querySeekHelpDetailById(Long id) {
        SeekHelpDO seekHelpDO = seekHelpMapper.querySeekHelpById(id);
        return this.seekHelpDOToVO(seekHelpDO);
    }


    private SeekHelpVO seekHelpDOToVO(SeekHelpDO seekHelpDO) {
        if (seekHelpDO == null)
            return null;
        SeekHelpVO seekHelpVO = new SeekHelpVO();
        BeanUtils.copyProperties(seekHelpDO, seekHelpVO);
        if (seekHelpDO.getImgId() != null) {
            List<Img> imgs = imgFeiClient.queryImagesByImgs(seekHelpDO.getImgId());
            if (imgs != null && !imgs.isEmpty()) {
                seekHelpVO.setImgPath(imgs.stream().findFirst().orElse(null).getPath());
            }
        }
        if (seekHelpDO.getDonationImgId() != null) {
            List<Img> imgs = imgFeiClient.queryImagesByImgs(seekHelpDO.getDonationImgId());
            if (imgs != null && !imgs.isEmpty()) {
                seekHelpVO.setDonationImgPath(imgs.stream().findFirst().orElse(null).getPath());
            }
        }
        if (seekHelpDO.getUsId() != null) {
            UserSimple userSimple = userFeiClient.queryUsById(seekHelpDO.getUsId());
            if (userSimple != null) {
                seekHelpVO.setUserSimple(userSimple);
            }
        }
        if (seekHelpDO.getMeId() != null) {
            Mechanism mechanism = mechanismFeiClient.queryByMechanismId(seekHelpDO.getMeId());
            if (mechanism != null) {
                seekHelpVO.setMechanism(mechanism);
            }
        }
        return seekHelpVO;
    }

    @Override
    public PageableDetails queryByUserId(Long userId, Pageable pageable) {
        int pageNumber = pageable.getPageNumber();
        int pageSize = pageable.getPageSize();
        Integer total = seekHelpMapper.queryCountSeekHelp(userId, null);
        if (total == 0) {
            ExceptionUtils.error(CommonCode.DATA_ERROR);
        }
        List<SeekHelpDO> seekHelpDOList = seekHelpMapper.querySeekHelp(userId, null, pageNumber * pageSize, pageSize);
        List<SeekHelpVO> seekHelpVOList = seekHelpDOList.stream().map(this::seekHelpDOToVO).collect(Collectors.toList());
        return new PageableDetails<>(pageNumber, pageSize, total, returnPages(total, pageSize), seekHelpVOList);
    }

    @Override
    public void updateSeekHelpDImg(SeekHelpDImgDTO seekHelpDImgDTO) {
        seekHelpMapper.updateSeekHelpDImg(seekHelpDImgDTO.getId(), seekHelpDImgDTO.getDonationImgId());
    }

    @Override
    public PageableDetails queryByMeId(Long meId, Pageable pageable) {
        int pageNumber = pageable.getPageNumber();
        int pageSize = pageable.getPageSize();
        Integer total = seekHelpMapper.queryCountSeekHelp(null, meId);
        if (total == 0) {
            ExceptionUtils.error(CommonCode.DATA_ERROR);
        }
        List<SeekHelpDO> seekHelpDOList = seekHelpMapper.querySeekHelp(null, meId, pageNumber * pageSize, pageSize);
        List<SeekHelpVO> seekHelpVOList = seekHelpDOList.stream().map(this::seekHelpDOToVO).collect(Collectors.toList());
        return new PageableDetails<>(pageNumber, pageSize, total, seekHelpVOList);
    }

    @Override
    public void create(SeekHelpDTO seekHelpDTO) {
        SeekHelpDO seekHelpDO = new SeekHelpDO();
        BeanUtil.copyProperties(seekHelpDTO, seekHelpDO);
        seekHelpDO.setCreateTime(new Date());
        seekHelpMapper.insertSeekHelp(seekHelpDO);

    }

    @Override
    public void updateMeRemark(SeekHelpDO seekHelpDO) {
        seekHelpMapper.updateMeRemark(seekHelpDO.getId(), seekHelpDO.getMeRemark());
    }

    @Override
    public void delete(Long id) {
        if (id == null)
            return;
        seekHelpMapper.delete(id);
    }

    @Override
    public void updateStatus(SeekHelpStatusDTO seekHelpStatusDTO) {
        seekHelpMapper.updateStatus(seekHelpStatusDTO.getId(), seekHelpStatusDTO.getStatus());
    }

    private int returnPages(int total, int pageSize) {
        //总页数
        return PageUtils.returnPages(total, pageSize);
    }
}
